package ca.abubakrsagar.breadboardsim;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JMenuBar;

/**
 * The controller for the menu bar.
 * 
 * @author Sagar
 * @since June 14, 2012
 * 
 */
public class MenuBarController implements ActionListener {

	private MenuBar menubar; // The menu bar object.

	/**
	 * Creates a new menu bar controller while associating the local menu bar
	 * with the specified menu bar.
	 * 
	 * @param menubar
	 *            The specified menu bar.
	 */
	public MenuBarController(MenuBar menubar) {
		this.menubar = menubar;
	}

	/**
	 * Perform actions based on which menu item has been clicked.
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == menubar.getOpen()) { // If open is clicked, allow
													// the user to load a file.
			try {
				menubar.openFileChooser();
			} catch (FileNotFoundException e1) {

				e1.printStackTrace();
			}
		} else if (e.getSource() == menubar.getSave()) { // If save is clicked,
															// allow the user to
															// save their
															// progress.
			try {
				menubar.saveFile();
			} catch (IOException e1) {

				e1.printStackTrace();
			}
		} else if (e.getSource() == menubar.getExit()) { // Close the program if
															// exit is clicked.
			System.exit(0);
		} else if (e.getSource() == menubar.getAbout()) { // Open the about menu
															// if about is
															// clicked.
			menubar.openAbout();
		}

	}

}
